package zee.com.customviewexample.activity

import android.view.Gravity
import android.view.View
import android.widget.*
import com.zee.popupWindow.MyPopupWindow
import com.zee.popupWindow.MyPopupWindowAdapter
import com.zee.utils.UIUtils
import com.zee.utils.ZEventBusUtils
import kotlinx.android.synthetic.main.activity_popupwindow.*
import org.greenrobot.eventbus.SubscribeMainThread
import zee.com.customviewexample.R

class ZxPopupWindowActivity : BaseActivity() {
    private lateinit var mPopupWindow: PopupWindow

    override fun getLayoutID(): Int {
        return R.layout.activity_popupwindow
    }

    override fun initViews() {
        //左上
        tv_popo_left.setOnClickListener {
            MyPopupWindow.init(R.layout.popu_more).setMyPopupWindowAdapter(object : MyPopupWindowAdapter() {
                override fun initViews(paView: View?) {
                }
            }).setMargin(15, 15).showAutoLocation(tv_popo_left);
        }

        //左下
        tv_popu_left_bottom.setOnClickListener {
            show_RightAndTop(tv_popu_left_bottom)
        }

        //显示中间
        tv_popu_mid.setOnClickListener {
            show_MidAndTop(tv_popu_mid)
        }

        //上中间
        tv_popo_mid.setOnClickListener {
            show_MidAndTop(tv_popo_mid)
        }

        //右上
        tv_popu_right.setOnClickListener {
            show_RightAndTop(tv_popu_right)
        }

        //右下
        tv_popu_right_bottom.setOnClickListener {
            MyPopupWindow.init(R.layout.popu_more,object : MyPopupWindowAdapter() {
                override fun initViews(paView: View?) {
                }
            }).showAsDropUp(tv_popu_right_bottom, 0, -0);
        }
    }

    private fun show_RightAndTop(paView: View) {
        MyPopupWindow.init(R.layout.popu_more,object : MyPopupWindowAdapter() {
            override fun initViews(paView: View?) {
                ZEventBusUtils.register(this)
                findViewByIdK(R.id.tv_item_1).setOnClickListener {
                    ZEventBusUtils.postTagNoParam("显示PopupWindow")
                    dismiss()
                }

                setOnClickListener(R.id.tv_item_1) {
                    ZEventBusUtils.postTagNoParam("显示PopupWindow")
                    dismiss()
                }
            }

            @SubscribeMainThread(tag = "显示PopupWindow")
            fun onEventBus() {
                UIUtils.showToastShort("显示出来了，天啊")
            }

            override fun onDestroy() {
                super.onDestroy()
                ZEventBusUtils.printCurAllSubscribers()
                ZEventBusUtils.unregister(this)
            }
        }).setMargin(16, 16).showAutoLocation(paView)
    }

    //上面的中间
    private fun show_MidAndTop(paView: View) {
        MyPopupWindow.newInstances(R.layout.popu_more).setMyPopupWindowAdapter(object : MyPopupWindowAdapter() {
            override fun initViews(paView: View?) {
                ZEventBusUtils.register(this)
                findViewByIdK(R.id.tv_item_1).setOnClickListener {
                    ZEventBusUtils.postTagNoParam("显示PopupWindow")
                    dismiss()
                }

                setOnClickListener(R.id.tv_item_1) {
                    ZEventBusUtils.postTagNoParam("显示PopupWindow")
                    dismiss()
                }
            }

            @SubscribeMainThread(tag = "显示PopupWindow")
            fun onEventBus() {
                UIUtils.showToastShort("显示出来了，天啊")
            }

            override fun onDestroy() {
                super.onDestroy()
                ZEventBusUtils.printCurAllSubscribers()
                ZEventBusUtils.unregister(this)
            }
        }).setMargin(0, 10).showAutoLocation(paView, Gravity.RIGHT or Gravity.TOP)

    }

}
